package com.m3839.sdk.pay.ui;

import android.os.Bundle;
import android.view.View;

import com.m3839.sdk.common.base.BaseDialogFragment;
import com.m3839.sdk.common.core.ErrorConstant;
import com.m3839.sdk.pay.bean.PayInfo;
import com.m3839.sdk.pay.bean.PayResult;
import com.m3839.sdk.pay.constant.PayConstant;
import com.m3839.sdk.pay.task.PayTask;

/**
 * Description:支付结果重新查询
 *
 * @author 陈宝阳
 * @create 2020/10/14 16:13
 */
public class PayRetryDialogFragment extends BaseDialogFragment {

  private PayResult payResult;
  private PayInfo payInfo;

  private PayTask payTask;

  public static PayRetryDialogFragment newInstance(PayResult payResult) {
    PayRetryDialogFragment fragment = new PayRetryDialogFragment();
    Bundle data = new Bundle();
    data.putParcelable(PayConstant.HYKB_KEY_PAY_RESULT, payResult);
    fragment.setArguments(data);
    return fragment;
  }

  public void setPayTask(PayTask payTask) {
    this.payTask = payTask;
  }

  @Override
  protected void parseArgs() {
    payResult = getArguments().getParcelable(PayConstant.HYKB_KEY_PAY_RESULT);
    payInfo = payResult.payInfo;
  }

  @Override
  protected String getLayoutName() {
    return "hykb_pay_retry_dialog_fragment";
  }

  @Override
  protected void initView(View view) {
  }

  @Override
  protected void setContent() {
    if (payInfo != null) {
      viewHolder.setTextView("tv_goods_name", payInfo.goodsName);
      viewHolder.setTextView("tv_pay_money",
              String.format(PayConstant.HYKB_STRING_MONEY_FORMAT, payInfo.money));
    }
    payTask.startPollingOrder(payResult.orderId);
  }

  @Override
  protected void initListener() {
    viewHolder.setViewClick("iv_pay_close",new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        PayResult result = new PayResult();
        result.payInfo = payInfo;
        result.resultCode = ErrorConstant.HYKB_CODE_PAY_RETRY_DIALOG_CLOSE;
        result.message = ErrorConstant.ERR_MSG_PAY_RETRY_DIALOG_CLOSE;
        payTask.handlePayCallBack(result);
        dismiss();
      }
    });
  }

}
